package com.linklogics.site;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.upfrost.Mapper;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.protocol.http.WebResponse;

import com.linklogics.site.OpenSessionInViewRequestCycle;

import rr.monitor.central.Central;
import rr.monitor.central.db.HibernateUtils;
import rr.monitor.central.db.LinkLogicsDAO;

/**
 * Implementation of the "Open Session In View" pattern for Wicket using UpFrost. Add this class
 * to your wicket project and add the following to the WebApplication class:
 * <pre>
 * @Override
 * public RequestCycle newRequestCycle(Request request, Response response)
 * {
 *   return new OpenSessionInViewRequestCycle(this, (WebRequest)request, (WebResponse)response, mapper);
 * }
 * </pre>
 * 
 * where mapper is the Mapper instance.
 * 
 * @author Rodrigo Reyes
 *
 */
public class OpenSessionInViewRequestCycle extends WebRequestCycle {

	static private Log log = LogFactory.getLog(OpenSessionInViewRequestCycle.class);
	
	private Mapper m_mapper;
	
	public OpenSessionInViewRequestCycle(WebApplication application, WebRequest request, Response response, Mapper mapper) 
	{
		super(application, request, response);
		m_mapper = mapper;
	}

	@Override
	protected void onBeginRequest() 
	{		
		super.onBeginRequest();
		m_mapper.initSession(request.getURL());
	}

	@Override
	protected void onEndRequest() 
	{
		super.onEndRequest();
		try {
			m_mapper.getSession().close();
		} catch (Exception exc)
		{
			log.error("Session already closed on " + request.getURL(), exc);
		}
	}

	
	
}
